//Usage:
// {
//   "from" : "22.622250,113.106636",
//     "to" : "23.020670,113.751790",
//       "maxrange": "10000"
// }
//return: 在范围内返回true，不在范围内返回false


// 云函数入口文件
const cloud = require('wx-server-sdk')
var QQMapWX = require('qqmap-wx-jssdk.js')
var qqmapsdk
var dist
var Calc2poisDist  = function (frompoi, topoi) {
  qqmapsdk = new QQMapWX({
    key: 'TAMBZ-IRWCX-FXE4V-ZU4RU-HQ3U7-3HBKL'
  });
   qqmapsdk.calculateDistance({
    mode: 'straight',
    from: frompoi,
    to: topoi,
    success: function(res){
      dist = res.result.elements[0].distance
    },
    fail: function (error) {
      console.error(error);
      dist = -1
    }
  })
  return dist
}

cloud.init()

// 云函数入口函数
exports.main = async (event, context) => {
  var dist
  const wxContext = cloud.getWXContext()
  dist = await Calc2poisDist(event.from, event.to)
  if(dist > event.maxrange || dist == -1){
    return false
  }else{
    return true
  }
}